/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.resourceInfo;

import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.DataTypeInterfaceGeneratorContext;
import com.inspur.edp.cef.spi.entity.resourceInfo.builinImpls.CefEntityResInfoImpl;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import java.util.ArrayList;
import java.util.List;

public class EntityResourceInfoGenerator extends DataTypeResourceInfoGenerator {

  public EntityResourceInfoGenerator(
      DataTypeInterfaceGeneratorContext parentContext, IGspCommonObject dataType) {
    super(parentContext, dataType);
  }

  @Override
  protected TypeInfo getParentClass() {
    return new TypeInfo(CefEntityResInfoImpl.class);
  }

  @Override
  protected final void createExtendMethodGenerators(List<ClassMethodGenerator> methods) {
    IGspCommonObject commonObject = (IGspCommonObject) dataType;
    if (commonObject.getContainConstraints() != null
        && commonObject.getContainConstraints().size() > 0)
      methods.add(getInitUQConsGenerator());
    if (commonObject.getParentObject() == null)
      methods.add(getIsRootGenerator());
    methods.add(new GetConfigIDGenerator(dataType));
//    methods.add(new GetDotNetConfigIDGenerator(dataType));
    if (!((IGspCommonObject) dataType).getContainChildObjects().isEmpty()) {
      methods.add(getInitChildInfosGenerator());
      if(((IGspCommonObject) dataType).getIDElement().getLabelID().equals("ID")==false)
        methods.add(new GetPrimaryKeyGenerator(((IGspCommonObject) dataType).getIDElement().getLabelID()));

    }
    if(((IGspCommonObject) dataType).getKeys()!=null&&((IGspCommonObject) dataType).getKeys().getCount()>0)
    {
      IGspCommonField field=dataType.findElement(((IGspCommonObject) dataType).getKeys().get(0).getSourceElement());
      if(field==null)
        throw new RuntimeException("在子节点【"+dataType.getName()+"】中，找不到与父节点关联的字段【"+((IGspCommonObject) dataType).getKeys().get(0).getSourceElement()+"】");
      if("ParentID".equals(field.getLabelID())==false)
        methods.add(new GetParentPropertyGenerator(field.getLabelID()));
    }
  }

  private ClassMethodGenerator getIsRootGenerator() {
    return new ResInfoIsRootGenerator((IGspCommonObject) dataType);
  }

  protected InitUQConsGenerator getInitUQConsGenerator() {
    return new InitUQConsGenerator((IGspCommonObject) dataType, parentContext);

  }


  protected ClassMethodGenerator getInitChildInfosGenerator(){
    return new InitChildInfosGenerator((IGspCommonObject) dataType, parentContext);
  }
}
